/**
 * 
 */
package com.module.osr.ui.stormbringer.charviewer.widgets;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.FlexibleImageBorder;
import com.dalonedrau.jogl.opengl.ui.border.StatsBorder;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.module.osr.stormbringer.bus.SBCharacter;

/**
 * @author Donald
 */
public final class StatsPanel extends PortraitPagePanel {
	/** the CHARISMA label. */
	private Label	lblCHA;
	/** the CONSTITUTION label. */
	private Label	lblCON;
	/** the DEXTERITY label. */
	private Label	lblDEX;
	/** the INTELLIGENCE label. */
	private Label	lblINT;
	/** the POWER label. */
	private Label	lblPOW;
	/** the SIZE label. */
	private Label	lblSIZ;
	/** the STRENGTH label. */
	private Label	lblSTR;
	/**
	 * Creates a new instance of {@link StatsPanel}.
	 */
	public StatsPanel() {
		super(Panel.PORTRAIT_CENTER);
		try {
			init();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
	/**
	 * Gets a "stats" label for display in the viewer.
	 * @return {@link Label}
	 */
	public Label getStatsLabel() {
		final int w = 24, h = 22;
		Label lbl = UIFactory.getInstance().getStandardH2Label("");
		lbl.setMaximumSize(w, h);
		lbl.setMinimumSize(w, h);
		lbl.setBorder(new StatsBorder());
		return lbl;
	}
	/**
	 * Gets a panel used to display a statistic.
	 * @param title the statistic's title
	 * @param lblVal the value label used
	 * @param tooltip the tooltip displayed when hovering over the panel
	 * @return {@link Panel}
	 */
	private Panel getStatsRow(final String title, final Label lblVal,
			final String tooltip) {
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		Label lbl = UIFactory.getInstance().getStandardH1Label(title);
		pnl.addChild(lbl);
		pnl.addChild(lblVal);
		pnl.setTooltipText(tooltip);
		return pnl;
	}
	/**
	 * Initializes the view.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		// 1st stats row
		Panel pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		lblSTR = getStatsLabel();
		pnl.addChild(getStatsRow(
				"STR",
				lblSTR,
				"Controls what weapons the character can carry, how much "
						+
						"they can carry, and how much damage they do in combat."
				));
		pnl.addChild(UIFactory.getInstance().getSpacer(20, 1));
		lblINT = getStatsLabel();
		pnl.addChild(getStatsRow(
				"INT",
				lblINT,
				"The ability to memorize, to solve problems, and to learn "
						+
						"from experience. Most useful in the areas of Magic and "
						+
						"Plant Lore."
				));
		super.addChild(pnl);
		super.addChild(UIFactory.getInstance().getSpacer(1, 4));
		// 2nd stats row
		pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		lblCON = getStatsLabel();
		pnl.addChild(getStatsRow(
				"CON",
				lblCON,
				"Determines how many hit points of damage the character can " +
						"take before dying"
				));
		pnl.addChild(UIFactory.getInstance().getSpacer(20, 1));
		lblPOW = getStatsLabel();
		pnl.addChild(getStatsRow(
				"POW",
				lblPOW,
				"Determines the ability to work magic.  Also affects luck " +
						"and willpower."
				));
		super.addChild(pnl);
		super.addChild(UIFactory.getInstance().getSpacer(1, 4));
		// 3rd stats row
		pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		lblSIZ = getStatsLabel();
		pnl.addChild(getStatsRow(
				"SIZ",
				lblSIZ,
				"The character's body mass. There are advantages to being " +
						"very large or very small, as well some disadvantages."
				));
		pnl.addChild(UIFactory.getInstance().getSpacer(20, 1));
		lblDEX = getStatsLabel();
		pnl.addChild(getStatsRow(
				"DEX",
				lblDEX,
				"The basic measure of agility, and a determing factor in the " +
						"weapons allowed to wield in combat."
				));
		super.addChild(pnl);
		super.addChild(UIFactory.getInstance().getSpacer(1, 4));
		// 4th stats row
		pnl = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		lblCHA = getStatsLabel();
		pnl.addChild(getStatsRow(
				"CHA",
				lblCHA,
				"A measure of leadership, charm, and of personality."
				));
		super.addChild(pnl);
		pnl = null;
		super.setBorder(new FlexibleImageBorder(
				new int[] {
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_bl_half"), // bottomleft
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_br_half"), // b-r
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_b_1"), // bottom
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_tl_half"), // top-l
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_tr_half"), // top-r
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_t_1"), // top
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_l"), // left
						SpriteImageObjectFactory.getInstance()
								.getImageRefId(
										"vx_flex_border_r") // right
				},
				new int[] {
						GlobalConsts.INT_005, // top
						GlobalConsts.INT_005, // left
						GlobalConsts.INT_005, // bottom
						GlobalConsts.INT_005 // right
				}
				));
	}
	/**
	 * Updates the view.
	 * @param pc the {@link SBCharacter} being viewed
	 */
	public void update(final SBCharacter pc) {
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		// STR
		if (pc.getFullStrength() == -999) {
			lblSTR.setText("");
		} else {
			sb.append((int) pc.getFullStrength());
			lblSTR.setText(sb.toString());
			sb.setLength(0);
		}
		// CON
		if (pc.getFullConstitution() == -999) {
			lblCON.setText("");
		} else {
			sb.append((int) pc.getFullConstitution());
			lblCON.setText(sb.toString());
			sb.setLength(0);
		}
		// SIZ
		if (pc.getFullSize() == -999) {
			lblSIZ.setText("");
		} else {
			sb.append((int) pc.getFullSize());
			lblSIZ.setText(sb.toString());
			sb.setLength(0);
		}
		// INT
		if (pc.getFullIntelligence() == -999) {
			lblINT.setText("");
		} else {
			sb.append((int) pc.getFullIntelligence());
			lblINT.setText(sb.toString());
			sb.setLength(0);
		}
		// POW
		if (pc.getFullPower() == -999) {
			lblPOW.setText("");
		} else {
			sb.append((int) pc.getFullPower());
			lblPOW.setText(sb.toString());
			sb.setLength(0);
		}
		// STR
		if (pc.getFullDexterity() == -999) {
			lblDEX.setText("");
		} else {
			sb.append((int) pc.getFullDexterity());
			lblDEX.setText(sb.toString());
			sb.setLength(0);
		}
		// CHA
		if (pc.getFullCharisma() == -999) {
			lblCHA.setText("");
		} else {
			sb.append((int) pc.getFullCharisma());
			lblCHA.setText(sb.toString());
			sb.setLength(0);
		}
		sb.returnToPool();
	}
}
